package tgc.edu.exam.controller;

import java.util.HashMap;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.entity.BookType;
import tgc.edu.exam.entity.Permission;
import tgc.edu.exam.form.BookTypeForm;
import tgc.edu.exam.service.BookTypeService;

@Controller
@RequestMapping("/bookType")
public class BookTypeController {

	@Autowired
	private BookTypeService bookTypeService;
	
	@RequestMapping("/manage")
	public String manage() {
		return "bookType/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public HashMap<String, Object> page(DataGridParam param,BookTypeForm form) {
		Pageable pageable = param.getPageable();
		Specification<BookType> spec = buildSpec(form);
		Page<BookType> page = bookTypeService.findAll(spec,pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<BookType> buildSpec(BookTypeForm form) {
		Specification<BookType> spec = new Specification<BookType>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<BookType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(form.getType())) {
					rules.add(cb.like(root.get("type"), "%"+form.getType()+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	@RequestMapping("/edit")
	public String edit(BookTypeForm form,ModelMap map) {
		BookType model = new BookType();
		if(form.getId()!=null) {
			model = bookTypeService.findById(form.getId());
		}
		map.put("model", model);
		return "bookType/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(BookTypeForm form,ModelMap map) {
		BookType model = new BookType();
		if(form.getId()!=null) {
			model=bookTypeService.findById(form.getId());
			BeanUtils.copyProperties(form, model);
			bookTypeService.save(model);
		}else {
			BeanUtils.copyProperties(form, model);
			bookTypeService.save(model);
		}
		return "保存成功";
		
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		try {
			bookTypeService.deleteById(id);
			return "删除成功！";
		}catch(Exception e){
			return "删除失败，外键约束";
		}
	}
}
